package ${project.packageName}.${project.dataCenterType}.ctrl;

import org.apache.hadoop.hbase.client.ResultScanner;
import javax.servlet.http.HttpServletRequest;
import java.util.List;
import org.apache.hadoop.hbase.TableName;
import java.text.ParseException;
import org.apache.hadoop.hbase.client.Result;
import org.apache.hadoop.hbase.util.Bytes;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;

import ${project.packageName}.${project.dataCenterType}.client.HbaseOperateClient;
import ${project.packageName}.${project.dataCenterType}.constant.BaseConstant;
import ${project.packageName}.${project.dataCenterType}.entity.JsonResult;
import ${project.packageName}.${project.dataCenterType}.util.ValidateRequestUtil;
import ${project.packageName}.${project.dataCenterType}.util.DateVerificationUtil;

@RestController
@RequestMapping("/${project.version}")
public class ${ctrlName}Controller {
    
    @Autowired
    private  HbaseOperateClient hbaseOperateClient;
    
    /**
     * 日志对象
     */
    private static final Logger LOG = LoggerFactory.getLogger(${ctrlName}Controller.class);
    
     /**
     * Description: ${restApi.description}
     <#list paraMaps as para>
     * @param ${para.name}
     </#list>
     */
    @RequestMapping(value = "${restApi.requestUrl}", method = RequestMethod.${restApi.requestType})
    public Object ${methodName}( HttpServletRequest request<#list paraMaps as para>, @RequestParam(required=${para.isMust}) ${para.type} ${para.name}</#list> ){
         if (!ValidateRequestUtil.validate(request)) {
            return new JsonResult(BaseConstant.CODE_TOKEN_ILLEGAL, BaseConstant.MSG_TOKEN_ILLEGAL);
        }
        
        try {
        
         ${startRowkeyStatements}
         ${endRowkeyStatements}
        List<String> monthBetween =DateVerificationUtil.getMonthBetween(startTime, endTime);
                
        List<TableName> targetTableNames = HbaseOperateClient.getTableNames();
        Long count = 0L;
            for (String monthStr : monthBetween) {
                String tableName = "${restApi.tableName}" + monthStr;

                if (!targetTableNames.contains(TableName.valueOf(tableName))) {
                    continue;
                }

                ResultScanner resultScanner = hbaseOperateClient.getResultScanner(start, end,
                    tableName);

                try {

                    for (Result result : resultScanner) {
                        count++ ;
                    }
                }
                catch (Exception e) {
                    LOG.error("报错信息" + e.getMessage());
                    continue;
                }
            }

            JSONObject returnObject = new JSONObject();
            returnObject.put("count", count);
            return JSON.toJSONString(returnObject);
        }
        catch (ParseException e) {
            LOG.error("报错信息：", e);
            return new JsonResult(BaseConstant.CODE_DATA_NONEXIST, BaseConstant.MSG_DATA_NONEXIST);
        }
        catch (Throwable e) {
            LOG.error("报错信息：", e);
            return new JsonResult(BaseConstant.CODE_ERROR, BaseConstant.MSG_ERROR);
        }

    }
}
